<!--字典管理页-->
<template>
  <!-- 字典数据搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="字典名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="searchForm.name"
                  @keydown.enter="selectDict"></el-input>
      </el-form-item>
      <el-form-item label="字典编码">
        <el-input placeholder="请输入" style="width:220px;" v-model="searchForm.code"
                  @keydown.enter="selectDict"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="selectDict">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:20px;">
    <div style="margin:10px 0 40px 10px;">
      字典列表
      <el-button type="success" style="float: right;margin-top:-3px;" @click="addDict">新增字典</el-button>
    </div>
    <el-table :data="dictArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="name" label="字典名称" align="center"/>
      <el-table-column prop="code" label="字典编码" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="showDictOption(scope.row.id)">{{ scope.row.code }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注信息" align="center"/>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editDict(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteDict(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 保存字典弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:500px;padding:40px;margin-top: 260px"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-form-item label="字典名称">
        <el-input placeholder="请输入" v-model="saveDictForm.name"></el-input>
      </el-form-item>
      <el-form-item label="字典编码">
        <el-input placeholder="请输入" v-model="saveDictForm.code"></el-input>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input placeholder="请输入" v-model="saveDictForm.remark"
                  @keydown.enter="saveDict"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveDict">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage, ElMessageBox} from "element-plus";
import qs from "qs";
import router from "@/router";

const searchForm = ref({name:'',code:''});
//定义对象用来保存最低点项弹窗表单数据
const saveDictForm = ref({name:'',code:'',remark:''});

const dialogVisible = ref(false);
const dialogTitle = ref('新增字典');
const handleClose = () =>{
  ElMessageBox.confirm('您确定要取消吗', '提示', {
    customClass: 'custom-message-box',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    Info('已取消保存');
    saveDictForm.value = {name:'',code:'',remark:''}
    dialogVisible.value = false;
  }).catch(() => {

  });
}
//定义数组对象用来保存字典列表表格数据
const dictArr = ref([])

//定义搜索字典的方法
const selectDict = () => {
  let data = qs.stringify(searchForm.value);
  axios.get(BASE_URL + '/v1/dict/select?' + data).then((response) => {
    if (response.data.code === 2000) {
      dictArr.value = response.data.data;
    } else {
      ElMessage({
        message: response.data.msg,
        type: 'error',
        showClose: true,
        center: true,
      })
    }
  })
}
onMounted(() => {
  selectDict();
})
//重置搜索的方法
const resetSearch = () => {
  searchForm.value = {name: '', code: ''}
  selectDict();
}

//新增字典的方法
const addDict = () =>{
  dialogVisible.value = true;
  dialogTitle.value = '新增字典';

}
//定义根据字典id显示字典项的方法
const showDictOption = (id) =>{
  router.push('/dictOption?id='+id)
}
//定义编辑字典的方法
const editDict = (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑字典';
  axios.get(BASE_URL + '/v1/dict/select?id=' + id)
      .then((response) => {
        if (response.data.code === 2000) {
          saveDictForm.value = response.data.data[0];
        } else {
          ElMessage({
            message: response.data.msg,
            type: 'error',
            showClose: true,
            center: true,
          });
        }
      })
}
//定义删除字典的方法
const deleteDict = (id) => {
  ElMessageBox.confirm('您确认删除该字典吗？', '提示', {
    customClass: 'custom-message-box',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    axios.delete(BASE_URL + '/v1/dict/delete/' + id)
        .then((response) => {
          if (response.data.code === 2000) {
            OKInfo('删除成功!')
            selectDict();
          } else {
            ElMessage({
              message: response.data.msg,
              type: 'error',
              showClose: true,
              center: true,
            })
          }
        })
  }).catch(() => {
    Info('已取消删除')
  });

}
//定义方法保存字典数据
const saveDict = () =>{
  if (saveDictForm.value.name === null || saveDictForm.value.name === '') {
    errorInfo('请输入字典名称');
    return;
  }
  if (saveDictForm.value.code === null || saveDictForm.value.code === '') {
    errorInfo('请输入字典编码');
    return;
  }
  let data = qs.stringify(saveDictForm.value);
  axios.post(BASE_URL + '/v1/dict/save?' + data).then((response) => {
    if (response.data.code === 2000) {
      OKInfo('保存成功!')
      dialogVisible.value = false;
      saveDictForm.value = {
        name: '',
        code: '',
        remark: ''
      }
      selectDict();
    } else {
      ElMessage({
        message: response.data.msg,
        type: 'error',
        showClose: true,
        center: true,
      })
    }
  })
}


</script>

<style scoped>

</style>